import { message } from 'antd';
import { history } from 'umi';
import { addVip,fetchSales,fetchCoach,updateVip,fetchVip,fetchConsume,fetchCourse,fetchBordyInfo,fetchIntegral,addBody,getMemberCard } from './service';
import moment from 'moment';

const Model = {
  namespace: 'cubeVip',
  state: {
    currentVip:{},
    vipCardList:[],
    salesList:[],
    coachList:[],
    consumeRecord:[],
    courseRecord:[],
    bordyRecord:[],
    integralRecord:[]
  },
  effects: {
    *add({ payload }, { call }) {
      let result=yield call(addVip, payload);
      if(result.code===0){
        message.success('提交成功');
        history.push(`/vip/center`)
      }else{
        message.error(result.message);
      }
    },
    *update({ payload }, { call }) {
      let result=yield call(updateVip, payload);
      if(result.code===0){
        message.success('提交成功');
        history.push(`/vip/center`)
      }else{
        message.error(result.message);
      }
    },
    *fetchVip({ payload }, { call,put }) {
      let result=yield call(fetchVip, payload);
      if(result.code===0){
        if(result.data.birthday){
          result.data.birthday=moment(result.data.birthday)
        }
        if(result.data.babybirthday){
          result.data.babybirthday=moment(result.data.babybirthday)
        }
        if(result.data.salesId){
          result.data.salesId=Number(result.data.salesId)
        }
        if(result.data.coachId){
          result.data.coachId=Number(result.data.coachId)
        }
        yield put({
          type: 'common',
          payload: {
            currentVip:result.data
          }
        });
      }else{
        message.error(result.message);
      }
    },
    *fetchSales({ payload }, { call,put }) {
      let result=yield call(fetchSales, payload);
      if(result.code===0){
        yield put({
          type: 'common',
          payload: {
            salesList:result.data
          }
        });
      }else{
        message.error(result.message);
      }
    },
    *fetchCoach({ payload }, { call,put }) {
      let result=yield call(fetchCoach, payload);
      if(result.code===0){
        yield put({
          type: 'common',
          payload: {
            coachList:result.data
          }
        });
      }else{
        message.error(result.message);
      }
    },
    *fetchConsume({ payload }, { call,put }) {
      let result=yield call(fetchConsume, payload);
      if(result.code===0){
        yield put({
          type: 'common',
          payload: {
            consumeRecord:result.data
          }
        });
      }else{
        message.error(result.message);
      }
    },
    *fetchCourse({ payload }, { call,put }) {
      let result=yield call(fetchCourse, payload);
      if(result.code===0){
        yield put({
          type: 'common',
          payload: {
            courseRecord:result.data
          }
        });
      }else{
        message.error(result.message);
      }
    },
    *fetchBordyInfo({ payload }, { call,put }) {
      let result=yield call(fetchBordyInfo, payload);
      if(result.code===0){
        yield put({
          type: 'common',
          payload: {
            bordyRecord:result.data
          }
        });
      }else{
        message.error(result.message);
      }
    },
    *fetchIntegral({ payload }, { call,put }) {
      let result=yield call(fetchIntegral, payload);
      if(result.code===0){
        yield put({
          type: 'common',
          payload: {
            integralRecord:result.data
          }
        });
      }else{
        message.error(result.message);
      }
    },
    *addBody({ payload }, { call }) {
      let result=yield call(addBody, payload);
      if(result.code===0){
        message.success('提交成功');
        history.push(`/vip/center/detail/${payload.memberId}`)
      }else{
        message.error(result.message);
      }
    },
    *getMemberCard({ payload }, { call,put }) {
      let result=yield call(getMemberCard, payload);
      if(result.code===0){
        yield put({
          type: 'common',
          payload: {
            vipCardList:result.data
          }
        });
      }else{
        message.error(result.message);
      }
    },
  },
  reducers: {
    common(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
  },
};
export default Model;
